import sqlite3
from GLOBALPATH import Global


if __name__ == '__main__':
    db = sqlite3.connect(Global.word2vec_db_path)
    cur = db.cursor()

    with open(Global.word2vec_txt_path, encoding='utf-8') as f:
        txt = f.readline()
        print(txt)
        i = 1
        while txt:
            txt = f.readline()
            word2vec = txt.split(' ', 1)
            # print(word2vec)

            # -------- 入库 -------------
            sql = 'INSERT INTO W2V (word, vec) VALUES ("{}", "{}")'.format(word2vec[0], word2vec[1])
            try:
                # 执行sql语句
                cur.execute(sql)
                if i % 1000 == 0:
                    # 提交到数据库执行
                    db.commit()
            except Exception as e:
                print(e)
                # Rollback in case there is any error
                db.rollback()
            # --------------------------

            i += 1
            print(i)

        # 关闭数据库连接
        cur.close()
        db.commit()
        db.close()
